package com.javasee.aiqueue;


import com.javasee.common.module.ModuleEnableConst;
import com.javasee.data.model.User;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

@Profile(ModuleEnableConst.AIQUEUE)
@Component
public class FileQueueSoa {

    @Autowired
    private AiDataFileService aiDataFileService;

    @Autowired
    private AiIndexFileService aiIndexFileService;


//    @Autowired
//    public FileStorageSoa(FileStorageService fileStorageService) {
//        this.fileStorageService = fileStorageService;
//    }

    public FileQueueSoa(){

    }

    public ResponseEntity<StoreResponse> store(Object object) throws IOException{
        return this.store(SerializationUtils.serialize(object));
    }

    public ResponseEntity<StoreResponse> store(byte[] data) throws IOException{
        AiDataFileService.WriteResult result = aiDataFileService.writeData(data);
        return ResponseEntity.ok(new StoreResponse(
                result.getWriteCount(),
                result.getOffset(),
                result.getLength(),
                "File stored successfully"
        ));
    }

    @PostConstruct
    public void init(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(()->{
            System.out.println("读数据已启动...");
            while (!Thread.currentThread().isInterrupted()){
                try {
                    AiIndexFileService.AiQueueIndex index = aiIndexFileService.readIndexOffset();
                    if(index == null){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        continue;
                    }

                    byte[] datas = aiDataFileService.readData(index);
                    if(datas == null || datas.length==0)continue;
                    List<User> userList = (List<User>) SerializationUtils.deserialize(datas);
                    System.out.println("读取数据的偏移量："+index.offset+"，读取的数据userList = " + userList);
                    aiIndexFileService.sync();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }




//    @GetMapping("/upload")
//    public ResponseEntity<UploadResponse> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
//        if (file.isEmpty()) {
//            return ResponseEntity.badRequest().build();
//        }
//
//        byte[] data = file.getBytes();
//        FileStorageService.WriteResult result = fileStorageService.writeData(data);
//
//        return ResponseEntity.ok(new UploadResponse(
//                result.getWriteCount(),
//                result.getOffset(),
//                result.getLength(),
//                "File stored successfully"
//        ));
//    }

    @ToString
    public static class StoreResponse {
        private final int writeCount;
        private final long offset;
        private final int length;
        private final String message;

        public StoreResponse(int writeCount, long offset, int length, String message) {
            this.writeCount = writeCount;
            this.offset = offset;
            this.length = length;
            this.message = message;
        }

        public int getWriteCount() {
            return writeCount;
        }

        public long getOffset() {
            return offset;
        }

        public int getLength() {
            return length;
        }

        public String getMessage() {
            return message;
        }
    }
}